//
//  VisitorLoginView.swift
//  iOS07-Weibo-LPK
//
//  Created by 罗培克 on 6/24/15.
//  Copyright © 2015 lpk. All rights reserved.
//

import UIKit

protocol VisitorLoginViewDelegate: NSObjectProtocol {
    func registerButtonClicked()
    func loginButtonClicked()
}

class VisitorLoginView: UIView {

    weak var delegate: VisitorLoginViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        // 设置背景色
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1.0)
        
        setUp()
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setUpViewWithInfo(imageName: String, message: String, isHome: Bool) {
        
        iconView.image = UIImage(named: imageName)
        titleLbl.text = message
        
        if isHome {
            homeIconView.hidden = false
        } else {
            homeIconView.hidden = true
            sendSubviewToBack(maskIconView)
        }
    }
    
    // MARK: - 动画
    func startAnim() {
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.duration = 20
        anim.toValue = M_PI * 2
        anim.repeatCount = MAXFLOAT
        
        iconView.layer.addAnimation(anim, forKey: nil)
    }
    
    // MARK: - 协议方法
    func register() {
//        print(__FUNCTION__)
        delegate?.registerButtonClicked()
    }
    
    func login() {
//        print(__FUNCTION__)
        delegate?.loginButtonClicked()
    }

    /// 添加子控件并布局
    func setUp() {
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeIconView)
        addSubview(titleLbl)
        addSubview(registerButton)
        addSubview(loginButton)
        
        /// iconView 的布局
        iconView.setTranslatesAutoresizingMaskIntoConstraints(false)
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0.0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: -80.0))
        
        /// homeIconView 的布局
        homeIconView.setTranslatesAutoresizingMaskIntoConstraints(false)
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: homeIconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0.0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: homeIconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0.0))
        
        /// maskIconView 的布局
        maskIconView.setTranslatesAutoresizingMaskIntoConstraints(false)
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[subVw]|", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subVw": maskIconView]))
        maskIconView.setTranslatesAutoresizingMaskIntoConstraints(false)
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[subVw]-240-|", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subVw": maskIconView]))
        
        /// titleLabel 的布局
        titleLbl.setTranslatesAutoresizingMaskIntoConstraints(false)
        addConstraint(NSLayoutConstraint(item: titleLbl, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0.0))
        addConstraint(NSLayoutConstraint(item: titleLbl, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 20.0))
        
        /// rigisterButton 的布局
        registerButton.setTranslatesAutoresizingMaskIntoConstraints(false)
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: titleLbl, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: 0.0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: titleLbl, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 20.0))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[subVw(100)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subVw": registerButton]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[subVw(40)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subVw": registerButton]))
        
        /// loginButton 的布局
        loginButton.setTranslatesAutoresizingMaskIntoConstraints(false)
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: titleLbl, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: 0.0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: titleLbl, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 20.0))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[subVw(100)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subVw": loginButton]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[subVw(40)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subVw": loginButton]))
        
    }
    
    // MARK: - 懒加载
    /// 图标
    lazy var iconView: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        return imageView
    }()
    
    /// 遮罩
    lazy var maskIconView: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        return imageView
    }()
    
    /// 房子
    lazy var homeIconView:UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        return imageView
    }()
    
    /// 文字标签
    lazy var titleLbl: UILabel = {
        let lbl = UILabel(frame: CGRectMake(0, 0, 240, 40))
        lbl.text = "登录后，最新、最热微博尽在掌握，不再会与实事潮流擦肩而过"
        lbl.textAlignment = NSTextAlignment.Center
        lbl.textColor = UIColor.darkGrayColor()
        lbl.preferredMaxLayoutWidth = 240
        lbl.numberOfLines = 2
        lbl.font = UIFont.systemFontOfSize(14.0)
        lbl.sizeToFit()
        
        return lbl
    }()
    
    /// 注册按钮
    lazy var registerButton: UIButton = {
        let btn = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        // 按钮点击事件
        btn.addTarget(self, action: "register", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
    }()
    
    /// 登录按钮
    lazy var loginButton: UIButton = {
        let btn = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
        btn.setTitle("登录", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        // 按钮点击事件
        btn.addTarget(self, action: "login", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
    }()
}
